using System;
using System.Linq;
using System.ComponentModel;
using IrisGenerator.Common.Tools;
using IrisGenerator.Common.Core;
using IrisGenerator.Common.DataTypes.Rules;
using IrisGenerator.Framework.RuleEngine;
using IrisGenerator.Framework.Events;

namespace IrisGenerator.Common.DataTypes
{


    public partial class Metatype : BindableObject<Metatype>,IRuleable<Metatype>, INotifyPropertyChanged
    {

        public bool IsDirty { get;private set; }


        public event EventHandler<BaseEventArgs<Metatype>> Inserting;
        public event EventHandler<BaseEventArgs<Metatype>> Inserted;
        public event EventHandler<BaseEventArgs<Metatype>> Deleting;

        public Metatype()
        {
            this.Rules = new MetatypeRule(this);
            this.Inserted += new EventHandler<BaseEventArgs<Metatype>>(Metatype_Inserted);
            this.Rules.MessageSend += new EventHandler<IrisGenerator.Framework.Messaging.MessageEventArgs<Metatype>>(Rules_MessageSend);
        }

        void Rules_MessageSend(object sender, IrisGenerator.Framework.Messaging.MessageEventArgs<Metatype> e)
        {
            this.OnMessageSend(e);
        }


        
        void Metatype_Inserted(object sender, BaseEventArgs<Metatype> e)
        {
            TargetCollection targets = Target.GetAll();
            foreach (Target t in targets)
            {
                MetatypeEquivalences me = new MetatypeEquivalences();
                me.TarId = t.TarId;
                me.MetId = e.Argument.MetId;
                me.MetEquType = "NOT DEFINED";
                me.Insert();
            }
        }

        protected void OnDeleting(BaseEventArgs<Metatype> e)
        {
            if (Deleting != null)
                Deleting(this, e);
        }

        protected void OnInserting(BaseEventArgs<Metatype> e)
        {
            if (Inserting != null)
                Inserting(this, e);
        }
        protected void OnInserted(BaseEventArgs<Metatype> e)
        {
            if (Inserted != null)
                Inserted(this, e);
        }
        protected void OnPropertyChanged(string PropertyName)
        {
            this.IsDirty = true;

            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
        

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion


        #region IRuleable Members

        public RuleBase<Metatype> Rules { get;private set; }
        
        public MetatypeRule ObjectRules
        {
            get
            {
                return this.Rules as MetatypeRule;
            }
        }

        #endregion

        public override bool Delete()
        {
            return this.Delete(false);
        }

        public static Metatype ConvertToMetatype(string TypeName, System.Nullable<System.Guid> TAR_ID)
        {
            IrisGenerator.Dal.MetatypeEquivalences dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.MetatypeEquivalences();
                System.Data.DataSet ds = dbo.ConvertToMetatype(TypeName, TAR_ID);
                Metatype obj = null;
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    if ((ds.Tables[0].Rows.Count > 0))
                    {
                        obj = new Metatype();
                        obj.Fill(ds.Tables[0].Rows[0]);
                    }
                }
                return obj;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public static Metatype SelectFromCode(string code)
        {
            MetatypeCollection col = Metatype.GetAll();
            Metatype result = col.Where(a => a.MetCode.Equals(code)).SingleOrDefault();
            return result;
        }
    }
}
